// ItemsMap.h: interface for the CItemsMap class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ITEMSMAP_H__28F5C403_695E_46A4_AD2C_EE13E4733C06__INCLUDED_)
#define AFX_ITEMSMAP_H__28F5C403_695E_46A4_AD2C_EE13E4733C06__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "CopyableMap.h"

template<class KEY, class ARG_KEY, class PROPERTY, class ARG_PROPERTY>
class CItemsMap : public CCopyableMap<KEY, ARG_KEY, PROPERTY, ARG_PROPERTY>
{
// Basics
public:
	CItemsMap()
	{}
	virtual ~CItemsMap()
	{}
// Attributes
private:
	CString m_strItemSeperator;
	CString m_strKeyValueSeperator;
// Operators
	CItemsMap & operator =(const CItemsMap &copyablemap)
	{
		m_strItemSeperator = copyablemap.m_strItemSeperator;
		m_strKeyValueSeperator = copyablemap.m_strKeyValueSeperator;
	return (CCopyableMap::operator =(copyablemap));
	}
// Functions
public:
	CItemsMap(const CString & strItems, const CString strItemSeperator =CString(';'), const CString strKeyValueSeperator =CString(':'))
		: m_strItemSeperator(strItemSeperator)
		, m_strKeyValueSeperator(strKeyValueSeperator)
	{
		CStringItemsArray items(strItems, m_strItemSeperator);
		for (int i= 0; i < items.GetSize(); i++)
		{
			CString &strItem= items[i];
			CStringItemsArray itemsKeyValue(strItem, strKeyValueSeperator);
			CString &strKey= itemsKeyValue[0];
			CString &strValue= (itemsKeyValue.GetSize()<2)?CString():itemsKeyValue[1];

			SetAt( KEY(strKey), PROPERTY(strValue) );
		}
	}
	CItemsMap(const CCopyableMap<KEY,ARG_KEY,PROPERTY,ARG_PROPERTY> &map, const CString strItemSeperator =CString(';'), const CString strKeyValueSeperator =CString(':'))
		: m_strItemSeperator(strItemSeperator)
		, m_strKeyValueSeperator(strKeyValueSeperator)
	{
		CCopyableMap<KEY,ARG_KEY,PROPERTY,ARG_PROPERTY>::operator =( map );
	}
	CString GetSerialData()
	{
		CString strSerialData;
		POSITION pos= GetStartPosition();
		while (pos)
		{
			KEY key;
			PROPERTY property;
			GetNextAssoc( pos, key, property );

			strSerialData += key + m_strKeyValueSeperator + property + m_strItemSeperator;
		}
	return (strSerialData);
	}
};

template<class KEY, class ARG_KEY, class PROPERTY, class ARG_PROPERTY>
CString & operator +=(CString &str, const CItemsMap<KEY,ARG_KEY,PROPERTY,ARG_PROPERTY> &items)
{
	return (str += items.GetSerialData());
}

template<class KEY, class ARG_KEY, class PROPERTY, class ARG_PROPERTY>
CString operator +(const CString &str, const CItemsMap<KEY,ARG_KEY,PROPERTY,ARG_PROPERTY> &items)
{
	return (str + items.GetSerialData());
}

template<class KEY, class ARG_KEY, class PROPERTY, class ARG_PROPERTY>
CString operator +(const CItemsMap<KEY,ARG_KEY,PROPERTY,ARG_PROPERTY> &items, const CString &str)
{
	return (items.GetSerialData() + str);
}

#endif // !defined(AFX_ITEMSMAP_H__28F5C403_695E_46A4_AD2C_EE13E4733C06__INCLUDED_)
